package com.demo2do.fms.admin.dao.supplier;

import java.util.Date;
import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.demo2do.fms.entity.appraisal.Appraisal;
import com.demo2do.fms.entity.appraisal.AppraisalResult;
import com.demo2do.fms.entity.appraisal.AppraisalType;
import com.demo2do.fms.entity.authority.Branch;
import com.demo2do.fms.entity.supplier.common.Supplier;

/**
 *
 * @author houtao
 *
 */
@Dao("hibernate")
public interface AppraisalResultDao {
    
    /**
     * 
     * @return
     */
    @Query("SELECT COUNT(*) FROM AppraisalResult WHERE disabled = false")
    public int count();
    
    /**
     * 
     * @param target
     * @return
     */
    @Query("SELECT COUNT(*) FROM AppraisalResult WHERE disabled = false AND target =:target")
    public int countByTarget(@Variable("target") String target);
    
    /**
     * 
     * @param targets
     * @return
     */
    @Query("SELECT COUNT(*) FROM AppraisalResult WHERE disabled = false AND target IN(:targets)")
    public int countByTarget(@Variable("targets") List<String> targets);
    
    /**
     * @param target
     * @param branch
     * @return
     */
    @Query("SELECT COUNT(*) FROM AppraisalResult WHERE disabled = false AND target =:target AND branch = :branch")
    public int countByTarget(@Variable("target") String target, @Variable("branch") Branch branch);
    
    /**
     * @param target
     * @param supplier
     * @return
     */
    @Query("SELECT COUNT(*) FROM AppraisalResult WHERE disabled = false AND target =:target AND supplier = :supplier")
    public int countByTarget(@Variable("target") String target, @Variable("supplier") Supplier supplier);
    
    /**
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM AppraisalResult WHERE disabled = false ORDER BY createTime DESC")
    public List<AppraisalResult> list(@Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * @param target
     * @return
     */
    @Query("FROM AppraisalResult WHERE disabled = false AND target =:target ORDER BY createTime DESC")
    public List<AppraisalResult> listByTarget(@Variable("target") String target);
    
    /**
     * @param target
     * @param branch
     * @return
     */
    @Query("FROM AppraisalResult WHERE disabled = false AND target =:target AND branch = :branch ORDER BY createTime DESC")
    public List<AppraisalResult> listByTarget(@Variable("target") String target, @Variable("branch") Branch branch);
    
    /**
     * @param target
     * @param supplier
     * @return
     */
    @Query("FROM AppraisalResult WHERE disabled = false AND target =:target AND supplier = :supplier ORDER BY createTime DESC")
    public List<AppraisalResult> listByTarget(@Variable("target") String target, @Variable("supplier") Supplier supplier);
    
    /**
     * @param target
     * @param timeAgo
     * @return
     */
    @Query("FROM AppraisalResult WHERE disabled = false AND target =:target AND createTime >= :timeAgo ORDER BY createTime ASC")
    public List<AppraisalResult> listByTarget(@Variable("target") String target, @Variable("timeAgo") Date timeAgo);
    
    /**
     * 
     * @param target
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM AppraisalResult WHERE disabled = false AND target =:target ORDER BY createTime DESC")
    public List<AppraisalResult> listByTarget(@Variable("target") String target, @Variable("beginIndex")int benginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * 
     * @param targets
     * @param benginIndex
     * @param maxResult
     * @return
     */
    @Query("FROM AppraisalResult WHERE disabled = false AND target IN (:targets)")
    public List<AppraisalResult> listByTarget(@Variable("targets") List<String> targets);

    /**
     * 
     * @param appraisal
     * @return
     */
    @Query("FROM AppraisalResult WHERE disabled = false AND appraisal = :appraisal ORDER BY createTime DESC")
    public List<AppraisalResult> list(@Variable("appraisal") Appraisal appraisal);

    /**
     * @param appraisal
     * @param target
     * @return
     */
    @Query("FROM AppraisalResult WHERE disabled = false AND appraisal = :appraisal AND target = :target")
    public AppraisalResult load(@Variable("appraisal") Appraisal appraisal, @Variable("target") String target);

	/**
	 * @param supplierId
	 * @param timeAgo
	 * @param appraisalType
	 * @return
	 */
    @Query("FROM AppraisalResult WHERE disabled = false AND target =:target AND createTime >= :timeAgo AND appraisalType = :appraisalType ORDER BY createTime ASC")
	public List<AppraisalResult> listByTarget(@Variable("target") String supplierId, @Variable("timeAgo") Date timeAgo, @Variable("appraisalType") AppraisalType appraisalType);
    
}
